#include <bitset>
#include <cstdint>
#include <iostream>
#include <istream>
#include <string>
using ll = int64_t;

const ll maxn = 1e7+7;
std::bitset<maxn> isnot;
ll nxtpos[maxn];

int main(){
    std::iostream::sync_with_stdio(false);
    std::cin.tie(nullptr);

    for(ll i=1;i<maxn;i++){
        const std::string str = std::to_string(i);
        for(char c:str){
            if(c=='7'){
                for(ll j=1;j*i<maxn;j++){
                    isnot[i*j]=true;
                }
                break;
            }
        }
    }
    for(ll i=maxn-1;i>=1;i--){
        static ll last = maxn;
        last = (isnot[i]?last:i);
        nxtpos[i]=last;
    }
    ll t;
    std::cin>>t;
    while(t--){
        ll x;
        std::cin>>x;
        if(isnot[x]){
            std::cout<<"-1\n";
            continue;
        }
        x++;
        std::cout<<nxtpos[x]<<"\n";
    }
}